<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<input type="file" id="test">
</body>
</html>
<script src="http://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
<script>

    function getIps(callback) {
        let ip_dups = {};
        const RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
        const noop = () => {};
        var mediaConstraints = {
            optional: [{RtpDataChannels: true}]
        };
        var servers = {iceServers:[{urls:"stun:stun.services.mozilla.com"}]};

        const ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;
        var pc = new RTCPeerConnection(servers, mediaConstraints);
        pc.createDataChannel('');
        pc.createOffer().then(function(sdp) {
            pc.setLocalDescription(sdp, noop, noop);
        })
        pc.onicecandidate = (ice) => {
            if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ip_regex)) return;
            console.log(ip_regex.exec(ice.candidate.candidate)[1])
        }
    }


    function excel(json, FileName, ext) {
        // const temp  = [json.title, ...json.data];
        // const ws = XLSX.utils.aoa_to_sheet(temp)
        // const wb = XLSX.utils.book_new();
        // XLSX.utils.book_append_sheet(wb, ws, 'sheet1');
        var tmpDown; //导出的二进制对象
        var bookType = (ext == undefined ? 'xlsx' : ext == 'xls' ? 'biff2' : ext );

        var keyMap = []; //获取keys

        json.title.map(function (value, index) {
            keyMap.push(value);
        });
        //数据处理
        var arr = [];
        keyMap.map(function (k, j) {
            var temp = Object.assign({}, {
                v: k,
                position: (j > 25 ? getCharCol(j) : String.fromCharCode(65 + j)) + '1'
            })
            arr.push(temp)
        })
        json.data.map(function (v, i) {
            v.map(function (k, n) {
                var temp = Object.assign({}, {
                    v: k,
                    position: (v > 25 ? getCharCol(j) : String.fromCharCode(65 + n)) + (i + 2)
                })
                arr.push(temp)
            })
        });

        var tmpdata = [];//用来保存转换好的json
        arr.map(function (v, i) {
            tmpdata[v.position] = {
                v: v.v,
                t: 's'
            }
        });

        var outputPos = Object.keys(tmpdata); //设置区域,比如表格从A1到D10
        var tmpWB = {
            SheetNames: ['mySheet'], //保存的表标题
            Sheets: {
                'mySheet': Object.assign({},
                    tmpdata, //内容
                    {
                        '!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] //设置填充区域
                    })
            },
            Props: {}
        };
        XLSX.writeFile(tmpWB, `${FileName}.${ext || 'xlsx'}`)
    };

    function s2ab(s) { //字符串转字符流
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }

    // 将指定的自然数转换为26进制表示。映射关系：[0-25] -> [A-Z]。
    function getCharCol(n) {
        var temCol = '',
            s = '',
            m = 0
        while (n > 0) {
            m = n % 26 + 1
            s = String.fromCharCode(m + 64) + s
            n = (n - m) / 26
        }
        return s
    }

    function s2ab(s) { //字符串转字符流
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }

    // 将指定的自然数转换为26进制表示。映射关系：[0-25] -> [A-Z]。
    function getCharCol(n) {
        var temCol = '',
            s = '',
            m = 0
        while (n > 0) {
            m = n % 26 + 1
            s = String.fromCharCode(m + 64) + s
            n = (n - m) / 26
        }
        return s
    }

    const list = {
        title: ['标题','内容'],
        data: [
            ['标题1', '内容1'],
            ['标题2', '内容2'],
            ['标题3', '内容3'],
        ]
    }
    // excel(list, '导出测试', 'xls');

document.querySelector("#test").onchange = function (e) {
    var render, t = e.target.files[0];
    render = new FileReader;
    render.onload = function(e) {
        info(XLSX.read(e.target.result, {
            type: "binary"
        }))
    }
    render.readAsBinaryString(t)

}
function info(e) {
    e.SheetNames.forEach(function(t) {
        var a = e.Sheets[t]
            , n = {
            raw: !0,
            header: ["name"],
            range: a["!ref"].replace(/\w\d+/, "A2")
        };
        r = XLSX.utils.sheet_to_json(a, n)
        console.log(r)
    });
}

</script>